<script setup>
  import { ref } from 'vue'
  import {artAddChannelService,artEditChannelService} from '@/api/article'
  const dialogVisible = ref(false)
  
  const formModel =ref({
    cate_name: '',
    cate_alias: ''
  })
  const rules = {
    cate_name:[
      { required: true, message: '请输入分类名称', trigger: 'blur' },
      {patten:/^\S{1,10}$/,message:"分类名称必须是1-10位的非空字符",trigger:'blur' }
    ],
    cate_alias:[
      { required: true, message: '请输入分类别名', trigger: 'blur' },
      {patten:/^[a-zA-Zo0-9]{1,15}$/,message:"分类别名必须是1-15位的字母或数字",trigger:'blur' }
    ]
  }

  const open = (row) => {
    dialogVisible.value = true
    formModel.value={...row}
  }

  const formRef = ref()
  const emit = defineEmits(['success'])
  const onSubmit = async () => {
    await formRef.value.validate()
    if(formModel.value.id){
      await artEditChannelService(formModel.value)
      ElMessage.success('编辑成功')
    }else{
      await artAddChannelService(formModel.value)
      ElMessage.success('添加成功')
    }
    dialogVisible.value = false
    emit('success')
  }

defineExpose({
  open
})
</script>

<template>
<el-dialog v-model="dialogVisible" :title="formModel.id?'编辑分类':'添加分类'" width="30%">
  <el-form :model="formModel" :rules="rules" ref="formRef" style="padding-right:30px" label-width="100px" >
    <el-form-item prop="cate_name" label="分类名称" >
      <el-input v-model="formModel.cate_name" placeholder="请输入分类名称"></el-input>
    </el-form-item>
    <el-form-item prop="cate_alias" label="分类别名" >
      <el-input v-model="formModel.cate_alias" placeholder="请输入分类别名"></el-input>
    </el-form-item>
  </el-form>
  <template #footer>
    <span class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button @click="onSubmit" type="primary"> 确认 </el-button>
    </span>
  </template>
</el-dialog>
</template>

<style>

</style>